#include "stdafx.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#include "log.h"

wxCriticalSection csPrint;
	


void Log::Initialize(const wxString &fileName, wxFile::OpenMode mode)
{
	if (mode != wxFile::OpenMode::write_append || !file.Open(fileName, mode))
		file.Create(fileName, true);
}

wxString nr(_T("\n"));

wxFile Log::file;
void Log::Info(const wxString fmt)
{
	Log::Info(fmt.c_str().AsWChar());
}


void Log::Info(const wxChar* fmt, ...)
{
	wxString format(fmt);

	const int bufLen = 2048;
	wxChar buf[bufLen];
	memset(buf, 0, bufLen);
    
	va_list args;
    va_start(args,fmt);
	int q = wxVsnprintf(buf, bufLen, fmt, args);
	va_end(args);
	
	wxString str(buf);

 //   va_start(args,fmt);
	//int i = str.PrintfV(format, args);
 //   va_end(args);

	csPrint.Enter();
	if (file.IsOpened())
	{
		file.Write(str);
		file.Write(nr);
	}
	else
		wxPrintf(str);
	csPrint.Leave();
}

void Log::Debug(const wxChar *fmt, ...)
{
    va_list args;
    va_start(args,fmt);
	Log::Info(fmt, args);
    va_end(args);

}